10D - LCIS - CodeForces Solution


dp *2800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
#define pii pair<ll,ll>
using namespace std;
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
const ll N=505;
ll n,m,a[N],b[N],dp[N][N],p[N][N],ans,pos;
stack<ll>stk;
int main(){
	n=read();
	rep(i,1,n)a[i]=read();
	m=read();
	rep(i,1,m)b[i]=read();
	a[0]=-1,b[0]=-1;
	rep(i,1,n){
		rep(j,1,m){
			if(a[i]==b[j]){
				rep(k,0,j-1){
					if(a[i]>b[k]&&dp[i][j]<dp[i-1][k]+1){
						dp[i][j]=dp[i-1][k]+1;
						p[i][j]=k;
					}
				}
			}
			else {
				dp[i][j]=dp[i-1][j];
				p[i][j]=p[i-1][j];
			}
		}
	}
	rep(i,1,m){
		if(dp[n][i]>ans)ans=dp[n][i],pos=i;
	}
	write(ans),putchar('\n');
	while(pos)stk.push(b[pos]),pos=p[n][pos];
	while(stk.size())write(stk.top()),putchar(' '),stk.pop();
	return 0;//
}
//11026622161443297331


Comments

Submit
0 Comments
More Questions

647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array